package com.easysoft.framework.dict.service;

import com.easysoft.commons.utils.DateTimeUtils;
import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.dict.mapper.SysDictionaryMapperExtd;
import com.easysoft.framework.dict.model.SysDictionary;
import com.easysoft.framework.dict.model.SysDictionaryQuery;
import com.easysoft.framework.dict.model.SysDictionaryView;
import com.easysoft.framework.friendlink.model.FriendLinkQuery;
import com.easysoft.framework.friendlink.model.FriendLinkView;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by try on 2017/8/1.
 */

@Service("service.SysDictionaryService")
public class SysDictionaryService {

    @Autowired
    @Qualifier("mapper.SysDictionaryMapperExtd")
    private SysDictionaryMapperExtd sysDictionaryMapperExtd;

    /**
     * 获取字典列表
     * @param page
     * @param query
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public List<SysDictionaryView> selectSysDictionaryListByPage(Page page, SysDictionaryQuery query) {
        int maxRow = sysDictionaryMapperExtd.selectSysDictionaryListCount(query);
        if (maxRow == 0) {
            return new ArrayList<SysDictionaryView>();
        }
        page.setMaxRow(maxRow);
        return sysDictionaryMapperExtd.selectSysDictionaryListByPage(page,query);
    }

    /**
     * 获取字典列表
     *
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public List<SysDictionaryView> selectSysDictionary() {
        return sysDictionaryMapperExtd.selectSysDictionary();
    }

    public List<SysDictionaryView> selectSysDictionaryWithoutParent(){
        return sysDictionaryMapperExtd.selectSysDictionaryWithoutParent();
    }

    /**
     * 添加数字字典
     * @param sysDictionary
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public int addSysDictionary(SysDictionary sysDictionary){
        sysDictionary.setId(UUIDUtils.getUUID());
        sysDictionary.setCreateTime(DateTimeUtils.getCurrentDate());
        return sysDictionaryMapperExtd.insertSelective(sysDictionary);
    }

    /**
     * 获取字典
     * @param id
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public SysDictionary selectSysDictionary(String id){
        return sysDictionaryMapperExtd.selectByPrimaryKey(id);
    }

    /**
     * 更新操作
     * @param sysDictionary
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public int updateSysDictionaryById(SysDictionary sysDictionary){
        return sysDictionaryMapperExtd.updateByPrimaryKeySelective(sysDictionary);
    }

}
